package tencent

import (
	"context"
	"fmt"
	sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20190711"
	types "shop_srvs/user_srv/service/sms"
)

type Service struct {
	appId     *string
	signature *string
	client    *sms.Client
}

func NewService(client *sms.Client, appId string, signName string) types.Service {
	return &Service{
		client:    client,
		appId:     &appId,
		signature: &signName,
	}
}
func (s *Service) Send(ctx context.Context, tplId string, args []string, numbers ...string) error {
	req := sms.NewSendSmsRequest()
	req.SmsSdkAppid = s.appId
	req.Sign = s.signature
	req.TemplateID = &tplId
	req.PhoneNumberSet = s.toStringPtrSlice(numbers...)

	req.TemplateParamSet = s.toStringPtrSlice(args...)
	resp, err := s.client.SendSms(req)
	if err != nil {
		return err
	}
	for _, status := range resp.Response.SendStatusSet {
		if status.Code == nil || *(status.Message) != "Ok" {
			return fmt.Errorf("发送短信失败 %s,%s", status.Code, status.Message)
		}
	}
	return nil
}
func (s *Service) toStringPtrSlice(nums ...string) []*string {
	var stringP []*string
	for _, number := range nums {
		stringP = append(stringP, &number)
	}
	return stringP
}

var _ types.Service = &Service{}
